package net.kevin.db.main;

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTParaRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShortHexNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblCellMar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPrEx;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHint;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblLayoutType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import net.kevin.db.model.CodeMaster;
import net.kevin.db.model.Foreignkey;
import net.kevin.db.model.Index;
import net.kevin.db.model.Master;
import net.kevin.db.model.TabColumn;
import net.kevin.db.model.Table;
import net.kevin.db.service.IOracleService;

/**
 * Description: POI 生成ORACLE数据库文档
 * @author: zhangfan15
 * Create Date: 2017年7月1日
 * <pre>
 * 修改记录:
 * 修改后版本			修改人		修改日期			修改内容 
 * 2017年7月1日.1		zhangfan15		2017年7月1日		create					
 * </pre>
 */
public class V1Doc {
	static {
		act = new ClassPathXmlApplicationContext("applicationContext.xml");
	}

	{
		oracleService = act.getBean(IOracleService.class);
	}

	public static final ApplicationContext act;

	private IOracleService oracleService;

	private Table table;

	private List<TabColumn> columns;

	private List<Foreignkey> foreignkeys;

	private List<Index> indexs;

	private List<CodeMaster> codeMasters;

	private String tmp = "template.docx";
	private XWPFDocument doc = new XWPFDocument();

	private String HEAD1 = "2";
	private String HEAD2 = "3";

	private String TABLE = "17";
	
	private boolean printTStyle = false; //模板文档样式日志
	private boolean printNStyle = false; //目标文档样式日志
	
	private boolean printTDocument = false; //模板文档内容日志
	private boolean printNDocument = false; //目标文档内容日志

	public V1Doc() throws Exception {
		XWPFStyles newStyles = doc.createStyles();
		// 关键行// 修改设置文档样式为静态块中读取到的样式
		newStyles.setStyles(getStyles());
	}

	private CTStyles getStyles() throws Exception {
		XWPFDocument template = new XWPFDocument(V1Doc.class.getClassLoader().getResourceAsStream(tmp));

		if(printTDocument)
			printLog(template.getDocument());
		
		CTStyles wordStyles = template.getStyle();
		
		if(printTStyle)
			printLog(wordStyles);

		template.close();

		return wordStyles;
	}

	public void title() {
		XWPFParagraph p = doc.createParagraph();
		p.setStyle(HEAD1);
		p.createRun().setText(table.getTableName());

		XWPFParagraph comments = doc.createParagraph();
		XWPFRun run = comments.createRun();
		run.setFontSize(12);
		run.setText(table.getComments());
	}

	/**
	 * Description : 外键
	 * 
	 * @Author: zhangfan15
	 * @Create Date: 2017年6月26日
	 */
	public void foreignkey() {
		XWPFParagraph p3 = doc.createParagraph();
		p3.setStyle(HEAD2);
		p3.createRun().setText("外键");

		XWPFTable table = doc.createTable(foreignkeys.size() + 1, 3);

		tableStyle(table);
		tableHeadStyle1(table, 0, 0);
		tableBodyStyle(table, 1);

		CTTblGrid tblGrid = table.getCTTbl().addNewTblGrid();
		tblGrid.addNewGridCol().setW(new BigInteger("2840"));
		tblGrid.addNewGridCol().setW(new BigInteger("2841"));
		tblGrid.addNewGridCol().setW(new BigInteger("2841"));

		table.getRow(0).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("Foregin Key Column");
		table.getRow(0).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("Table");
		table.getRow(0).getCell(2).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("column");

		for (int i = 0, size = foreignkeys.size(); i < size; i++) {
			int row = i + 1;
			Foreignkey foreignkey = foreignkeys.get(i);

			table.getRow(row).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(foreignkey.getForeginKeyColumn());
			table.getRow(row).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(foreignkey.getTableName());
			table.getRow(row).getCell(2).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(foreignkey.getColumnName());
		}
	}

	/**
	 * Description :字段描述
	 * 
	 * @Author: zhangfan15
	 * @Create Date: 2017年6月26日
	 */
	public void fieldDesc() {
		XWPFParagraph p3 = doc.createParagraph();
		p3.setStyle(HEAD2);
		p3.createRun().setText("字段描述");

		XWPFTable table = doc.createTable(columns.size() + 1, 5);

		tableStyle(table);
		tableHeadStyle1(table, 0, 0);
		tableBodyStyle(table, 1, "left");

		CTTblGrid tblGrid = table.getCTTbl().addNewTblGrid();
		tblGrid.addNewGridCol().setW(new BigInteger("1942"));
		tblGrid.addNewGridCol().setW(new BigInteger("1719"));
		tblGrid.addNewGridCol().setW(new BigInteger("810"));
		tblGrid.addNewGridCol().setW(new BigInteger("1305"));
		tblGrid.addNewGridCol().setW(new BigInteger("2746"));

		table.getRow(0).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("name");
		table.getRow(0).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("type");
		table.getRow(0).getCell(2).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("nullable");
		table.getRow(0).getCell(3).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("Default/expr");
		table.getRow(0).getCell(4).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("comments");

		for (int i = 0, size = columns.size(); i < size; i++) {
			int row = i + 1;
			TabColumn column = columns.get(i);

			table.getRow(row).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(column.getColumnName());
			table.getRow(row).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(column.getDataType());
			table.getRow(row).getCell(2).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(column.getNullAble());
			table.getRow(row).getCell(3).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue(column.getExpr());
			table.getRow(row).getCell(4).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(column.getComments());
		}
	}

	/**
	 * Description :索引
	 * 
	 * @Author: zhangfan15
	 * @Create Date: 2017年6月26日
	 */
	public void index() {
		XWPFParagraph p3 = doc.createParagraph();
		p3.setStyle(HEAD2);
		p3.createRun().setText("索引");

		XWPFTable table = doc.createTable(indexs.size() + 1, 3);

		tableStyle(table);
		tableHeadStyle1(table, 0, 0);
		tableBodyStyle(table, 1);

		CTTblGrid tblGrid = table.getCTTbl().addNewTblGrid();
		tblGrid.addNewGridCol().setW(new BigInteger("2840"));
		tblGrid.addNewGridCol().setW(new BigInteger("2841"));
		tblGrid.addNewGridCol().setW(new BigInteger("2841"));
		table.getRow(0).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("Index name");
		table.getRow(0).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("Index type");
		table.getRow(0).getCell(2).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("columns");

		for (int i = 0, size = indexs.size(); i < size; i++) {
			int row = i + 1;
			Index index = indexs.get(i);
			table.getRow(row).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(index.getIndexName());
			table.getRow(row).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue(index.getType());
			table.getRow(row).getCell(2).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(index.getColumns());
		}
	}

	/**
	 * Description :数据字典
	 * 
	 * @Author: zhangfan15
	 * @Create Date: 2017年6月26日
	 */
	public void codeMaster() {
		XWPFParagraph p3 = doc.createParagraph();
		p3.setStyle(HEAD2);
		p3.createRun().setText("数据字典");

		for (CodeMaster codeMaster : codeMasters) {
			XWPFTable table = doc.createTable(codeMaster.getMaster().size() + 4, 3);

			tableStyle(table);
			tableHeadStyle2(table, 0, 2);
			tableHeadStyle3(table, 3, 3);
			tableBodyStyle(table, 4, "center");

			CTTblGrid tblGrid = table.getCTTbl().addNewTblGrid();
			tblGrid.addNewGridCol().setW(new BigInteger("2840"));
			tblGrid.addNewGridCol().setW(new BigInteger("2841"));
			tblGrid.addNewGridCol().setW(new BigInteger("2841"));

			table.getRow(0).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("字段");
			table.getRow(0).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(codeMaster.getColumnName());
			table.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); // 合并开始列
			table.getRow(0).getCell(2).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); // 需要合并的列

			table.getRow(1).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("字段编码");
			table.getRow(1).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(codeMaster.getMasterCode());
			table.getRow(1).getCell(1).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); // 合并开始列
			table.getRow(1).getCell(2).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); // 需要合并的列

			table.getRow(2).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("名称");
			table.getRow(2).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT()
					.setStringValue(codeMaster.getMasterName());
			table.getRow(2).getCell(1).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); // 合并开始列
			table.getRow(2).getCell(2).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); // 需要合并的列

			table.getRow(3).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("代码");
			table.getRow(3).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("名称");
			table.getRow(3).getCell(2).getCTTc().getPArray(0).getRArray(0).addNewT().setStringValue("状态");

			for (int i = 0, size = codeMaster.getMaster().size(); i < size; i++) {
				int row = i + 4;
				Master master = codeMaster.getMaster().get(i);
				table.getRow(row).getCell(0).getCTTc().getPArray(0).getRArray(0).addNewT()
						.setStringValue(master.getCode());
				table.getRow(row).getCell(1).getCTTc().getPArray(0).getRArray(0).addNewT()
						.setStringValue(master.getName());
				table.getRow(row).getCell(2).getCTTc().getPArray(0).getRArray(0).addNewT()
						.setStringValue(master.getStatus());
			}

			doc.createParagraph();
		}
	}

	public void tableStyle(XWPFTable table) {
		CTTbl tbl = table.getCTTbl();
		CTTblPr tblPr = tbl.getTblPr();

		CTString style = tblPr.getTblStyle() != null ? tblPr.getTblStyle() : tblPr.addNewTblStyle();
		style.setVal(TABLE);

		CTTblWidth w = tblPr.getTblW() != null ? tblPr.getTblW() : tblPr.addNewTblW();
		w.setType(STTblWidth.Enum.forString("dxa"));
		w.setW(new BigInteger("8522"));

		CTTblWidth ind = tblPr.getTblInd() != null ? tblPr.getTblInd() : tblPr.addNewTblInd();
		ind.setType(STTblWidth.Enum.forString("dxa"));
		ind.setW(new BigInteger("0"));

		CTTblBorders borders = tblPr.getTblBorders() != null ? tblPr.getTblBorders() : tblPr.addNewTblBorders();

		CTBorder topB = borders.getTop() != null ? borders.getTop() : borders.addNewTop();
		topB.setColor("auto");
		topB.setSpace(new BigInteger("0"));
		topB.setSz(new BigInteger("4"));
		topB.setVal(STBorder.Enum.forString("single"));

		CTBorder leftB = borders.getLeft() != null ? borders.getLeft() : borders.addNewLeft();
		leftB.setColor("auto");
		leftB.setSpace(new BigInteger("0"));
		leftB.setSz(new BigInteger("4"));
		leftB.setVal(STBorder.Enum.forString("single"));

		CTBorder bottomB = borders.getBottom() != null ? borders.getBottom() : borders.addNewBottom();
		bottomB.setColor("auto");
		bottomB.setSpace(new BigInteger("0"));
		bottomB.setSz(new BigInteger("4"));
		bottomB.setVal(STBorder.Enum.forString("single"));

		CTBorder rightB = borders.getRight() != null ? borders.getRight() : borders.addNewRight();
		rightB.setColor("auto");
		rightB.setSpace(new BigInteger("0"));
		rightB.setSz(new BigInteger("4"));
		rightB.setVal(STBorder.Enum.forString("single"));

		CTBorder insideHB = borders.getInsideH() != null ? borders.getInsideH() : borders.addNewInsideH();
		insideHB.setColor("auto");
		insideHB.setSpace(new BigInteger("0"));
		insideHB.setSz(new BigInteger("4"));
		insideHB.setVal(STBorder.Enum.forString("single"));

		CTBorder insideVB = borders.getInsideV() != null ? borders.getInsideV() : borders.addNewInsideV();
		insideVB.setColor("auto");
		insideVB.setSpace(new BigInteger("0"));
		insideVB.setSz(new BigInteger("4"));
		insideVB.setVal(STBorder.Enum.forString("single"));

		CTTblLayoutType layout = tblPr.getTblLayout() != null ? tblPr.getTblLayout() : tblPr.addNewTblLayout();
		layout.setType(STTblLayoutType.Enum.forString("fixed"));

		CTShortHexNumber look = tblPr.getTblLook() != null ? tblPr.getTblLook() : tblPr.addNewTblLook();
		look.setVal(hexStringToBytes("04A0"));

		CTTblCellMar tblCellMar = tblPr.getTblCellMar() != null ? tblPr.getTblCellMar() : tblPr.addNewTblCellMar();

		CTTblWidth top = tblCellMar.getTop() != null ? tblCellMar.getTop() : tblCellMar.addNewTop();
		top.setType(STTblWidth.Enum.forString("dxa"));
		top.setW(new BigInteger("0"));

		CTTblWidth left = tblCellMar.getLeft() != null ? tblCellMar.getLeft() : tblCellMar.addNewLeft();
		left.setType(STTblWidth.Enum.forString("dxa"));
		left.setW(new BigInteger("108"));

		CTTblWidth bottom = tblCellMar.getBottom() != null ? tblCellMar.getBottom() : tblCellMar.addNewBottom();
		bottom.setType(STTblWidth.Enum.forString("dxa"));
		bottom.setW(new BigInteger("0"));

		CTTblWidth right = tblCellMar.getRight() != null ? tblCellMar.getRight() : tblCellMar.addNewRight();
		right.setType(STTblWidth.Enum.forString("dxa"));
		right.setW(new BigInteger("108"));

		for (XWPFTableRow row : table.getRows()) {
			rowStyle(row);
		}
	}

	public void rowStyle(XWPFTableRow row) {
		CTTblPrEx prex = row.getCtRow().getTblPrEx() != null ? row.getCtRow().getTblPrEx()
				: row.getCtRow().addNewTblPrEx();

		CTTblBorders borders = prex.getTblBorders() != null ? prex.getTblBorders() : prex.addNewTblBorders();

		CTBorder topB = borders.getTop() != null ? borders.getTop() : borders.addNewTop();
		topB.setColor("auto");
		topB.setSpace(new BigInteger("0"));
		topB.setSz(new BigInteger("4"));
		topB.setVal(STBorder.Enum.forString("single"));

		CTBorder leftB = borders.getLeft() != null ? borders.getLeft() : borders.addNewLeft();
		leftB.setColor("auto");
		leftB.setSpace(new BigInteger("0"));
		leftB.setSz(new BigInteger("4"));
		leftB.setVal(STBorder.Enum.forString("single"));

		CTBorder bottomB = borders.getBottom() != null ? borders.getBottom() : borders.addNewBottom();
		bottomB.setColor("auto");
		bottomB.setSpace(new BigInteger("0"));
		bottomB.setSz(new BigInteger("4"));
		bottomB.setVal(STBorder.Enum.forString("single"));

		CTBorder rightB = borders.getRight() != null ? borders.getRight() : borders.addNewRight();
		rightB.setColor("auto");
		rightB.setSpace(new BigInteger("0"));
		rightB.setSz(new BigInteger("4"));
		rightB.setVal(STBorder.Enum.forString("single"));

		CTBorder insideHB = borders.getInsideH() != null ? borders.getInsideH() : borders.addNewInsideH();
		insideHB.setColor("auto");
		insideHB.setSpace(new BigInteger("0"));
		insideHB.setSz(new BigInteger("4"));
		insideHB.setVal(STBorder.Enum.forString("single"));

		CTBorder insideVB = borders.getInsideV() != null ? borders.getInsideV() : borders.addNewInsideV();
		insideVB.setColor("auto");
		insideVB.setSpace(new BigInteger("0"));
		insideVB.setSz(new BigInteger("4"));
		insideVB.setVal(STBorder.Enum.forString("single"));

		CTTblLayoutType layout = prex.getTblLayout() != null ? prex.getTblLayout() : prex.addNewTblLayout();

		layout.setType(STTblLayoutType.Enum.forString("fixed"));

		CTShortHexNumber look = prex.getTblLook() != null ? prex.getTblLook() : prex.addNewTblLook();
		look.setVal(hexStringToBytes("04A0"));

		CTTblCellMar tblCellMar = prex.getTblCellMar() != null ? prex.getTblCellMar() : prex.addNewTblCellMar();

		CTTblWidth top = tblCellMar.getTop() != null ? tblCellMar.getTop() : tblCellMar.addNewTop();
		top.setType(STTblWidth.Enum.forString("dxa"));
		top.setW(new BigInteger("0"));

		CTTblWidth left = tblCellMar.getLeft() != null ? tblCellMar.getLeft() : tblCellMar.addNewLeft();
		left.setType(STTblWidth.Enum.forString("dxa"));
		left.setW(new BigInteger("108"));

		CTTblWidth bottom = tblCellMar.getBottom() != null ? tblCellMar.getBottom() : tblCellMar.addNewBottom();
		bottom.setType(STTblWidth.Enum.forString("dxa"));
		bottom.setW(new BigInteger("0"));

		CTTblWidth right = tblCellMar.getRight() != null ? tblCellMar.getRight() : tblCellMar.addNewRight();
		right.setType(STTblWidth.Enum.forString("dxa"));
		right.setW(new BigInteger("108"));
	}

	public void tableHeadStyle1(XWPFTable table, int beginRow, int endRow) {
		List<XWPFTableRow> rows = table.getRows();
		for (int i = beginRow; i <= endRow; i++) {
			XWPFTableRow row = rows.get(i);
			List<XWPFTableCell> cells = row.getTableCells();
			row.getCtRow().addNewTrPr().addNewJc().setVal(STJc.Enum.forString("center"));
			for (XWPFTableCell cell : cells) {
				CTTc tc = cell.getCTTc();
				
				CTTcPr tcPr = tc.getTcPr() != null ? tc.getTcPr() : tc.addNewTcPr();
				CTVerticalJc vAlign = tcPr.getVAlign() != null ? tcPr.getVAlign() : tcPr.addNewVAlign();
				vAlign.setVal(STVerticalJc.Enum.forString("center"));

				CTP p = tc.getPArray(0);
				p.setRsidP(hexStringToBytes("00AB1750"));
				p.setRsidR(hexStringToBytes("00AB1750"));
				p.setRsidRDefault(hexStringToBytes("00AB1750"));
				p.setRsidRPr(hexStringToBytes("001317D1"));

				CTPPr ppr = p.addNewPPr();
				ppr.addNewJc().setVal(STJc.Enum.forString("center"));

				CTParaRPr rpr = ppr.addNewRPr();

				CTFonts fonts = rpr.addNewRFonts();
				fonts.setAscii("黑体");
				fonts.setEastAsia("黑体");
				fonts.setHAnsi("黑体");

				rpr.addNewB();

				rpr.addNewSz().setVal(new BigInteger("28"));
				rpr.addNewSzCs().setVal(new BigInteger("28"));

				CTR r = p.addNewR();
				r.setRsidRPr(hexStringToBytes("001317D1"));

				CTRPr rpr0 = r.addNewRPr();

				CTFonts fonts0 = rpr0.addNewRFonts();
				fonts0.setAscii("黑体");
				fonts0.setEastAsia("黑体");
				fonts0.setHAnsi("黑体");
				fonts0.setHint(STHint.Enum.forString("eastAsia"));

				rpr0.addNewB();

				rpr0.addNewSz().setVal(new BigInteger("28"));
				rpr0.addNewSzCs().setVal(new BigInteger("28"));
			}
		}
	}

	public void tableHeadStyle2(XWPFTable table, int beginRow, int endRow) {
		List<XWPFTableRow> rows = table.getRows();
		for (int i = beginRow; i <= endRow; i++) {
			XWPFTableRow row = rows.get(i);
			List<XWPFTableCell> cells = row.getTableCells();
			row.getCtRow().addNewTrPr().addNewJc().setVal(STJc.Enum.forString("center"));
			for (int j = 0, size = cells.size(); j < size; j++) {
				XWPFTableCell cell = cells.get(j);
				CTTc tc = cell.getCTTc();
				
				CTTcPr tcPr = tc.getTcPr() != null ? tc.getTcPr() : tc.addNewTcPr();
				CTVerticalJc vAlign = tcPr.getVAlign() != null ? tcPr.getVAlign() : tcPr.addNewVAlign();
				vAlign.setVal(STVerticalJc.Enum.forString("center"));

				CTP p = tc.getPArray(0);
				p.setRsidP(hexStringToBytes("00AB1750"));
				p.setRsidR(hexStringToBytes("00AB1750"));
				p.setRsidRDefault(hexStringToBytes("00AB1750"));
				p.setRsidRPr(hexStringToBytes("001317D1"));

				CTPPr ppr = p.addNewPPr();
				ppr.addNewJc().setVal(STJc.Enum.forString("center"));

				CTParaRPr rpr = ppr.addNewRPr();

				CTFonts fonts = rpr.addNewRFonts();
				fonts.setAscii("黑体");
				fonts.setEastAsia("黑体");
				fonts.setHAnsi("黑体");
				fonts.setHint(STHint.Enum.forString("eastAsia"));

				if (j == 0) {
					rpr.addNewB();
					rpr.addNewSz().setVal(new BigInteger("28"));
					rpr.addNewSzCs().setVal(new BigInteger("28"));
				} else {
					rpr.addNewSz().setVal(new BigInteger("18"));
					rpr.addNewSzCs().setVal(new BigInteger("18"));
				}

				CTR r = p.addNewR();
				r.setRsidRPr(hexStringToBytes("001317D1"));

				CTRPr rpr0 = r.addNewRPr();

				CTFonts fonts0 = rpr0.addNewRFonts();
				fonts0.setAscii("黑体");
				fonts0.setEastAsia("黑体");
				fonts0.setHAnsi("黑体");
				fonts0.setHint(STHint.Enum.forString("eastAsia"));

				if (j == 0) {
					rpr0.addNewB();
					rpr0.addNewSz().setVal(new BigInteger("28"));
					rpr0.addNewSzCs().setVal(new BigInteger("28"));
				} else {
					rpr0.addNewSz().setVal(new BigInteger("18"));
					rpr0.addNewSzCs().setVal(new BigInteger("18"));
				}
			}
		}
	}

	public void tableHeadStyle3(XWPFTable table, int beginRow, int endRow) {
		List<XWPFTableRow> rows = table.getRows();
		for (int i = beginRow; i <= endRow; i++) {
			XWPFTableRow row = rows.get(i);
			List<XWPFTableCell> cells = row.getTableCells();
			row.getCtRow().addNewTrPr().addNewJc().setVal(STJc.Enum.forString("center"));
			for (XWPFTableCell cell : cells) {
				CTTc tc = cell.getCTTc();
				
				CTTcPr tcPr = tc.getTcPr() != null ? tc.getTcPr() : tc.addNewTcPr();
				CTVerticalJc vAlign = tcPr.getVAlign() != null ? tcPr.getVAlign() : tcPr.addNewVAlign();
				vAlign.setVal(STVerticalJc.Enum.forString("center"));

				CTP p = tc.getPArray(0);
				p.setRsidP(hexStringToBytes("00AB1750"));
				p.setRsidR(hexStringToBytes("00AB1750"));
				p.setRsidRDefault(hexStringToBytes("00AB1750"));
				p.setRsidRPr(hexStringToBytes("001317D1"));

				CTPPr ppr = p.addNewPPr();
				ppr.addNewJc().setVal(STJc.Enum.forString("center"));

				CTParaRPr rpr = ppr.addNewRPr();

				CTFonts fonts = rpr.addNewRFonts();
				fonts.setAscii("黑体");
				fonts.setEastAsia("黑体");
				fonts.setHAnsi("黑体");

				rpr.addNewB();

				rpr.addNewSz().setVal(new BigInteger("28"));
				rpr.addNewSzCs().setVal(new BigInteger("28"));

				CTR r = p.addNewR();
				r.setRsidRPr(hexStringToBytes("001317D1"));

				CTRPr rpr0 = r.addNewRPr();

				CTFonts fonts0 = rpr0.addNewRFonts();
				fonts0.setAscii("黑体");
				fonts0.setEastAsia("黑体");
				fonts0.setHAnsi("黑体");
				fonts0.setHint(STHint.Enum.forString("eastAsia"));

				rpr0.addNewB();
				rpr.addNewI();

				rpr0.addNewSz().setVal(new BigInteger("28"));
				rpr0.addNewSzCs().setVal(new BigInteger("28"));
			}
		}
	}

	/**
	 * Description : 表格样式
	 * 
	 * @param table
	 * @Author: zhangfan15
	 * @Create Date: 2017年6月29日
	 */
	public void tableBodyStyle(XWPFTable table, int bodyRow) {
		tableBodyStyle(table, bodyRow, "left");
	}

	public void tableBodyStyle(XWPFTable table, int bodyRow, String jc) {
		List<XWPFTableRow> rows = table.getRows();
		for (int i = bodyRow, size = rows.size(); i < size; i++) {
			XWPFTableRow row = rows.get(i);
			List<XWPFTableCell> cells = row.getTableCells();

			for (XWPFTableCell cell : cells) {
				CTTc tc = cell.getCTTc();

				CTTcPr tcPr = tc.getTcPr() != null ? tc.getTcPr() : tc.addNewTcPr();
				CTVerticalJc vAlign = tcPr.getVAlign() != null ? tcPr.getVAlign() : tcPr.addNewVAlign();
				vAlign.setVal(STVerticalJc.Enum.forString("center"));

				CTP p = tc.getPArray(0);

				p.setRsidP(hexStringToBytes("00AB1750"));
				p.setRsidR(hexStringToBytes("00AB1750"));
				p.setRsidRDefault(hexStringToBytes("00AB1750"));
				p.setRsidRPr(hexStringToBytes("001317D1"));

				CTPPr ppr = p.addNewPPr();

				CTParaRPr rpr = ppr.addNewRPr();

				ppr.addNewJc().setVal(STJc.Enum.forString(jc));

				CTFonts fonts = rpr.addNewRFonts();
				fonts.setAscii("黑体");
				fonts.setEastAsia("黑体");
				fonts.setHAnsi("黑体");

				rpr.addNewSz().setVal(new BigInteger("18"));
				rpr.addNewSzCs().setVal(new BigInteger("18"));

				CTR r = p.addNewR();
				r.setRsidRPr(hexStringToBytes("001317D1"));

				CTRPr rpr0 = r.addNewRPr();

				CTFonts fonts0 = rpr0.addNewRFonts();
				fonts0.setAscii("黑体");
				fonts0.setEastAsia("黑体");
				fonts0.setHAnsi("黑体");
				fonts0.setHint(STHint.Enum.forString("eastAsia"));

				rpr0.addNewSz().setVal(new BigInteger("18"));
				rpr0.addNewSzCs().setVal(new BigInteger("18"));
			}
		}
	}

	public byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	private byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	
	public boolean isPrintTStyle() {
		return printTStyle;
	}

	public void setPrintTStyle(boolean printTStyle) {
		this.printTStyle = printTStyle;
	}

	public boolean isPrintNStyle() {
		return printNStyle;
	}

	public void setPrintNStyle(boolean printNStyle) {
		this.printNStyle = printNStyle;
	}

	public boolean isPrintTDocument() {
		return printTDocument;
	}

	public void setPrintTDocument(boolean printTDocument) {
		this.printTDocument = printTDocument;
	}

	public boolean isPrintNDocument() {
		return printNDocument;
	}

	public void setPrintNDocument(boolean printNDocument) {
		this.printNDocument = printNDocument;
	}
	
	private void printLog(Object...args){
		System.out.println(">>>日志开始");
		System.out.println(args[0]);
		System.out.println(">>>日志结束");
	}

	/**
	 * Description :
	 * @param user 属主
	 * @param tables 表
	 * @param path 生成文档路径
	 * @throws Exception
	 * @Author: zhangfan15
	 * @Create Date: 2017年6月26日
	 */
	public void createDoc(String user, String[] tables, String path) throws Exception {
		FileOutputStream out = new FileOutputStream(path);

		for (String t : tables) {
			this.table = oracleService.getTable(user, t);
			this.columns = oracleService.getColumn(user, t);
			this.indexs = oracleService.getIndex(user, t);
			this.foreignkeys = oracleService.getForeignkey(user, t);
			this.codeMasters = oracleService.getCodeMaster(user, t);
			
			if (table != null) {
				title();
			}
			if (foreignkeys != null) {
				foreignkey();
			}
			if (columns != null) {
				fieldDesc();
			}
			if (indexs != null) {
				index();
			}			
			if (codeMasters != null) {
				codeMaster();
			}
		}
		
		if(printTDocument)
			printLog(doc.getDocument());
		
		doc.write(out);
		out.close();
		doc.close();
	}
}
